<?php

namespace app\mp\controller;

use app\common\entity\Result;
use app\common\service\BannerService;
use app\common\service\RecommendService;
use app\common\service\TopicService;
use app\common\service\VideoAndTopicService;
use app\common\service\VideoService;
use app\Request;
use think\facade\Cache;

class Index extends Base
{
    /**
     * 获取用户设置和后台推荐的视频和专栏的合集 排序按照热度排序
     * @param Request $request
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $page = (int)$request->param('page', 1);
        $service = new RecommendService();
        $res = $service->recommend($this->user['id'], $page);
        return Result::success($res);
    }

    /**
     * 前台-轮播图
     * @param BannerService $bannerService
     * @return \think\Response
     */
    public function banner(BannerService $bannerService)
    {
        $res = $bannerService->getList(1, 6, ['status' => 1, 'position' => 1]);
        return Result::success($res);
    }

    /**
     * 按照热门排序获取 专栏或者视频
     * @param Request $request
     * @return \think\Response
     */
    public function hot(Request $request)
    {
        $page = (int)$request->param('page', 1);
        $type = $request->param('type', 'all');
        switch ($type) {
            // 视频
            case 'video':
                $service = new VideoService();
                $result = $service->getList($page, 20, [], 'hot')['list'];
                break;
            // 专栏
            case 'topic':
                $service = new TopicService();
                $result = $service->getList($page, 20, [], 'hot')['list'];
                break;
            // 其他类型都为查询两者
            default:
                $service = new VideoAndTopicService();
                $result = $service->select(['order' => 'hot', 'page' => $page, 'size' => 20]);
                break;
        }
        return Result::success($result);
    }

    /**
     * 按照时间排序获取 专栏或者视频
     * @param Request $request
     * @return \think\Response
     */
    public function recent(Request $request)
    {
        $page = (int)$request->param('page', 1);
        $service = new VideoAndTopicService();
        $result = $service->recent($page, 20);
        return Result::success($result);
    }


    public function store()
    {
        echo Cache::get("appname");
        echo "<br />";
        echo Cache::get("filename");
    }

    public function third(Request $request)
    {
        $code = $request->param('code');
    }
}
